﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class SGRepository : ISGRepository
    {
        private readonly MyDbContext myDbContext;

        public SGRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public async Task<bool> AddSgAsync(SG sg)
        {
            myDbContext.SG.Add(sg);
            return await myDbContext.SaveChangesAsync() > 0;
        }

        public SGFenYe SelectSGFenYe(int page, int pageSize)
        {
            int totalRecords = myDbContext.SG.Where(sg => sg.CheckStatus == null).Count(); // 获取数据总量
            var Sg = myDbContext.SG.Where(sg=>sg.CheckStatus==null).Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return new SGFenYe
            {
                totalRecords = totalRecords,
                sg = Sg
            };
        }

        public SGFenYe DateSGAsync(string salaryGrantId, string gzc, DateTime startDate, DateTime endDate, int page, int pageSize)
        {
            var query = myDbContext.SG.AsQueryable();

            if (!string.IsNullOrEmpty(salaryGrantId))
            {
                query = query.Where(s => s.SalaryGrantId == salaryGrantId);
            }
            if (!string.IsNullOrEmpty(gzc))
            {
                var searchTerm = gzc; // 可以将需要查找的字符串存储在一个变量中，提高可读性  
                query = query.Where(s =>
                    s.FirstKindName.Contains(searchTerm) ||
                    s.SecondKindName.Contains(searchTerm) ||
                    s.ThirdKindName.Contains(searchTerm));
            }

            if (startDate != default && endDate != default)
            {
                query = query.Where(s => s.RegistTime >= startDate && s.RegistTime <= endDate);
            }

            int totalRecords = query.Count();
            var sg = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return new SGFenYe
            {
                totalRecords = totalRecords,
                sg = sg
            };
        }
        public async Task<bool> RemoveSGAsync(string SalaryGrantId, double totalCostS, string Checker)
        {
            var hfToModify = await myDbContext.SG.FirstOrDefaultAsync(sg => sg.SalaryGrantId == SalaryGrantId);
            if (hfToModify != null)
            {
                hfToModify.SalaryPaidSum = totalCostS;
                hfToModify.Checker = Checker;
                hfToModify.CheckTime= DateTime.Now;
                hfToModify.CheckStatus = 1;
                myDbContext.Attach(hfToModify);
                myDbContext.Entry(hfToModify).State = EntityState.Modified;
                await myDbContext.SaveChangesAsync();
                return true;
            }
            return false;
        }
    }
}
